package com.situ.mymall.controller;

import com.situ.mymall.pojo.entity.Category;
import com.situ.mymall.pojo.query.CategoryQuery;
import com.situ.mymall.pojo.vo.CategoryCountVO;
import com.situ.mymall.service.ICategoryService;
import com.situ.mymall.util.PageResult;
import com.situ.mymall.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @RequestMapping("/list")
    public PageResult list(CategoryQuery categoryQuery){

        return categoryService.list(categoryQuery);
    }

    @RequestMapping("/selectById")
    public Result selectById(Integer id){
        Category category = categoryService.selectById(id);
        return Result.ok(category);
    }

    @RequestMapping("/addSelective")
    public Result addSelective(Category category){
        categoryService.addSelective(category);
        return Result.ok("添加成功");
    }

    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        categoryService.deleteById(id);
        return Result.ok("删除成功");
    }

    @RequestMapping("/deleteAll")
    public Result deleteAll(Integer[] ids){
        categoryService.deleteAll(ids);
        return Result.ok("删除成功");
    }

    @RequestMapping("/update")
    public Result update(Category category){
        categoryService.update(category);
        return Result.ok("更新成功");
    }

    @RequestMapping("/updateStatus")
    public Result updateStatus(Category category){
        categoryService.updateStatus(category);
        return Result.ok("更新成功");
    }

    //查找一级分类
    @RequestMapping("/selectTopCategory")
    public Result selectTopCategory(){
        List<Category> list = categoryService.selectTopCategory();
        return Result.ok(list);
    }

    //查找二级分类
    @RequestMapping("/selectSecondCategoryByTopCategoryId")
    public Result selectSecondCategoryByTopCategoryId(Integer id){
        List<Category> list = categoryService.selectSecondCategoryByTopCategoryId(id);
        return Result.ok(list);
    }

    //Echarts
    @RequestMapping("/echarts")
    public Result echarts(){
        //CategoryCountVO里面的数据必须定义为name、value
        //List<CategoryCountVO> list = categoryService.echarts();
        //System.out.println("list: " + list); //[null, null, null, null, null, null]
        //return Result.ok(list);
        return Result.ok(categoryService.echarts());
    }

}
